package com.lxy.phonebook.interceptor;

import com.lxy.phonebook.constant.SystemConstant;
import com.lxy.phonebook.entity.CheckResult;
import com.lxy.phonebook.utils.JwtUtils;
import com.lxy.phonebook.utils.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
 * 自定义的拦截器，用来验证token
 */
public class SysInterceptor implements HandlerInterceptor {

    //使用日志
    private final static Logger logger = LoggerFactory.getLogger(SysInterceptor.class);

    //在请求到达控制器之前，进行拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        logger.info("执行了拦截的核心方法");
        //获取页面的请求地址
        String contextPath = request.getRequestURI();
        logger.info("路径" + contextPath);
        //1.验证请求是否是请求方法的类型
        if (handler instanceof HandlerMethod) {
            //2.从请求中获取token
            String token = request.getHeader("token");
            System.out.println("token = " + token);
            //3.验证token是否为空
            //StringUtils
            if (StringUtils.isEmpty(token)) {
                logger.info("验证失败！");
                print(response, R.error(SystemConstant.JWT_ERRCODE_NULL, "签名验证不存在！"));
                return false;
            } else {
                //4.解析验证token
                //JwtUtils.validateJWT(token)如果验证成功会给checkResult的success复制true，不成功就是false
                CheckResult checkResult = JwtUtils.validateJWT(token);
                //5.如果验证成功
                //判断checkResult中的值，上面解密后赋进去的
                if (checkResult.isSuccess()) {
                    //通过拦截器
                    logger.info("签名验证通过...");
                    return true;
                } else {
                    //6.验证不成功
                    //返回相应的code和信息
                    //SystemConstant里面设置相应的静态常量初始值
                    switch (checkResult.getErrCode()) {
                        case SystemConstant.JWT_ERRCODE_EXPIRE:
                            logger.info("签名失效！");
                            print(response, R.error(SystemConstant.JWT_ERRCODE_EXPIRE, "签名失效！"));
                            break;
                        case SystemConstant.JWT_ERRCODE_FAIL:
                            logger.info("签名验证不通过！");
                            print(response, R.error(SystemConstant.JWT_ERRCODE_FAIL, "签名验证不通过！"));
                            break;
                        default:
                            break;
                    }
                    return false;
                }
            }
        }
        return true;
    }

    //自定义响应方法，将请求处理结果响应回浏览器
    public void print(HttpServletResponse response, Object message) {
        try {
            //HttpStatus.OK.value()成功的状态码
            response.setStatus(HttpStatus.OK.value());
            response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
            //设置响应头
            response.setHeader("Cache-Control","no-cache, must-revalidate");
            //在config包下面创建WebAppConfigurer.java，进行拦截配置调用链，代码如下：
            PrintWriter writer = response.getWriter();
            writer.write(message.toString());
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
